<template>
  <div class="app-container">
    <el-form style="float:left">
      <el-form-item label="标签组名称" :label-width="formLabelWidth" class="inputWidth">
        <el-input v-model="labelGroupNameSearch" placeholder="请输入标签组名"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary"  icon="el-icon-search" style="float:left;margin-left:10px" @click="getList">查询</el-button>
    <el-button type="primary" icon="el-icon-plus" @click="addLabelGroup" style="float:right">添加标签组</el-button>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row max-height="550" class="groupTable">
      <el-table-column align="center" label='标签组名称' >
        <template slot-scope="scope">
          {{scope.row.labelGroupName}}
        </template>
      </el-table-column>
      <el-table-column label="是否单选" align="center">
        <template slot-scope="scope">
          {{scope.row.isUnique=='1'?'是':'否'}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="350" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editLabelGroup(scope)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteLabelGroup(scope)">删除</el-button>
          <el-button type="info" size="mini" @click="setLabelItem(scope)">标签项配置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="size">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { labelGroupList, labelGroupDelete } from 'api/label'
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      labelGroupNameSearch: '',
      size: 0,
      pageSize: 10,
      curPage: 1,
      formLabelWidth: '100px'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const parm = {
        pageSize: this.pageSize,
        curPage: this.curPage - 1,
        labelGroupName: this.labelGroupNameSearch
      }
      try {
        let back = await labelGroupList(parm)
        back = back.data
        if (back.resultCode === '200') {
          this.list = back.result
          this.size = back.size
          this.listLoading = false
        }
      } catch (err) {
        this.$alert('请求失败', '提示')
      }
    },
    addLabelGroup() {
      this.$router.push({ name: 'labelGroupAdd' })
    },
    editLabelGroup(scope) {
      this.$router.push({ name: 'labelGroupEdit', query: { labelGroupId: scope.row.labelGroupId }})
    },
    async deleteLabelGroup(scope) {
      try {
        let back = await labelGroupDelete(scope.row.labelGroupId)
        back = back.data
        if (back.resultCode === '200') {
          this.getList()
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    setLabelItem(scope) {
      this.$router.push({ name: 'labelItemList', query: { 'labelGroupName': scope.row.labelGroupName, 'isUnique': scope.row.isUnique, 'labelGroupId': scope.row.labelGroupId }})
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },
    handleCurrentChange(val) {
      this.curPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
  .groupTable{
    margin-top:20px;
  }
  .block{
    float: right;
    margin-top:10px;
  }
</style>
